package com.hnevc.wechatdemo;


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    //创建实例
    FindFragment findFragment;
    MyFragment myFragment;
    WeChatFragment weChatFragment;
    ContactsFragment contactsFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findFragment = new FindFragment();
        myFragment = new MyFragment();
        weChatFragment = new WeChatFragment();
        contactsFragment = new ContactsFragment();
    }

    public void buttonClick(View view) {
        //切换不同的Fragment
        int id=view.getId();//获取点击事件的触发控件的id
        switch (id){
            case R.id.id_wechat:{

                //获取fragmentManager，通过getSupportFragmentManager获取
                FragmentManager fm = getSupportFragmentManager();
                //开启事务，调用beginTransaction开启
                FragmentTransaction transaction = fm.beginTransaction();
                //向容器中添加（add）或替换（replace）FindFragment；需要容器
                transaction.replace(R.id.id_container,weChatFragment);
                //提交事务
                transaction.commit();
                break;
            }
            case R.id.id_contacts:{

                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                transaction.replace(R.id.id_container,contactsFragment);
                transaction.commit();

                break;
            }
            case R.id.id_find:{

                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                transaction.replace(R.id.id_container,findFragment);
                transaction.commit();

                break;
            }
            case R.id.id_my:{
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                transaction.replace(R.id.id_container,myFragment);
                transaction.commit();
                break;
            }
        }
    }
}
